package caidan;

import caidan.data.CategoryData;
import caidan.data.FoodData;
import caidan.res.Resource;
import jinyilw.common.Direction;
import jinyilw.swing.FontTools;
import jinyilw.common.Operator;
import jinyilw.swing.ScreenTools;
import jinyilw.swing.WindowTools;
import jinyilw.swing.visuals.Icomp.IRichLabel;
import jinyilw.swing.visuals.Jcomp.TransRichButton;
import jinyilw.swing.visuals.Rcomp.RDialog;

import javax.swing.Icon;
import javax.swing.JFrame;
import javax.swing.SwingConstants;
import java.awt.Color;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.Window;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.ArrayList;

public class FavoriteView extends RDialog
{
	private static final long serialVersionUID = -7195737034738553648L;
	public static final Color redColor = new Color(255, 50, 50);
	private CategoryData[] categoryDatas;
	private TransRichButton[] foodButtons;
	private FCDialog[] fcDialogs;
	long ct1 = 0, ct2 = 0, sT;
	int cX, cY;

	private final JFrame owner;

	public FavoriteView(JFrame jf)
	{
		super(jf);
		owner = jf;
		setOptionStyle(100, 0.95f);
		setWindowSymbol(Resource.ratingIcon);
		setTitle("查看菜单喜爱度");
		setTitleHeight(30);
		setTitleColor(Color.yellow);
		setTitleFont(FontTools.getCustomFont(21));

		Container contentPanel = getContentPane();
		contentPanel.setLayout(new FlowLayout());

		categoryDatas = MainApp.getCategoryDatas();
		foodButtons = new TransRichButton[categoryDatas.length];
		fcDialogs = new FCDialog[categoryDatas.length];
		for (int i = 0; i < foodButtons.length; i++)
		{
			foodButtons[i] = new TransRichButton(
					categoryDatas[i].getCategoryName() + "喜爱度");
			foodButtons[i].setFont(FontTools.getBigFont());
			foodButtons[i].setForeground(redColor);
			contentPanel.add(foodButtons[i]);

			foodButtons[i].setImageLocation(SwingConstants.SOUTH);
			foodButtons[i].setImage(Resource.getLazyScaleIcon(
					MainApp.imagePath + MainApp.imageNames[i]));
			int ii = i;
			foodButtons[i].addClickOperater(new Operator()
			{
				int j = ii;
				@Override
				public void operate(Object o)
				{
					if (fcDialogs[j] == null)
						fcDialogs[j] = new FCDialog(owner, categoryDatas[j]);
					fcDialogs[j].setWindowSymbol(Resource.getLazyCustomIcon(
							MainApp.imagePath + MainApp.imageNames[j], 25, 25));
					fcDialogs[j].setLocationRelativeTo(owner);
					fcDialogs[j].updateData();
					fcDialogs[j].setVisible(true);
				}
			});
			categoryDatas[i].addNameChangedOperater(new Operator<String>()
			{
				int j = ii;
				@Override
				public void operate(String name)
				{
					foodButtons[j].setText(name + "喜爱度");
				}
			});
		}

		addMouseListener(new MouseAdapter()
		{
			@Override
			public void mouseClicked(MouseEvent e)
			{
				WindowTools.hideWindowToBorderInThread(FavoriteView.this,
						Direction.UP);
			}

			@Override
			public void mousePressed(MouseEvent e)
			{
				cX = FavoriteView.this.getX();
				cY = FavoriteView.this.getY();
				ct1 = System.currentTimeMillis();
			}
		});
		// addMoveReleaseOperater(new MoveReleaserOperater());
		setSize(1250, 360);
		setLocationRelativeTo(null);
	}

	//	public FCDialog getDialog(int categoryID)
	//	{
	//		return fcDialogs[categoryID];
	//	}

	//	public TransRichButton getButton(int categoryID)
	//	{
	//		return foodButtons[categoryID];
	//	}

	//	public void updateName(int categoryID)
	//	{
	//		foodButtons[categoryID]
	//				.setText(categoryDatas[categoryID].getCategoryName() + "喜爱度");
	//		if (fcDialogs[categoryID] != null)
	//			fcDialogs[categoryID].updateTitile();
	//	}
	//
	//	public void addNew(int categoryID)
	//	{
	//		if (fcDialogs[categoryID] != null)
	//			fcDialogs[categoryID].addNew();
	//	}
	//
	//	public void delete(int categoryID)
	//	{
	//		if (fcDialogs[categoryID] != null)
	//			fcDialogs[categoryID].delete();
	//	}

	@Override
	public void setSize(int width, int height)
	{
		if (foodButtons == null)
		{
			super.setSize(width, height);
			return;
		}
		int ss = 10;
		int ww = (width - 160) / foodButtons.length - ss;
		int hh = ww * 7 / 9;
		int xx = 30, yy = 30;
		Dimension size = new Dimension(ww, hh);
		for (int i = 0; i < foodButtons.length; i++)
		{
			foodButtons[i].setPreferredSize(size);
			//			caidanPanels[i].setBounds(xx, yy, ww, hh);
			xx += ss + ww;
		}
		width = xx - 15;
		height = yy + hh + 50;
		super.setSize(width, height);
	}

	//	private class MoveReleaserOperater implements Operater
	//	{
	//		@Override
	//		public void operate(Object o)
	//		{
	//			ct2 = System.currentTimeMillis();
	//			sT = ct2 - ct1;
	//			if (sT > 100 && sT < 350)
	//			{
	//				int x = FavoriteView.this.getX();
	//				int y = FavoriteView.this.getY();
	//				int xx = x - cX;
	//				int yy = y - cY;
	//				if (Math.abs(xx) >= Math.abs(yy))
	//				{
	//					if (xx > 0)
	//						WindowTools.hideWindowToBorderInThread(
	//								FavoriteView.this, Direction.RIGHT);
	//					else
	//						WindowTools.hideWindowToBorderInThread(
	//								FavoriteView.this, Direction.LEFT);
	//				} else
	//				{
	//					if (yy > 0)
	//						WindowTools.hideWindowToBorderInThread(
	//								FavoriteView.this, Direction.DOWN);
	//					else
	//						WindowTools.hideWindowToBorderInThread(
	//								FavoriteView.this, Direction.UP);
	//				}
	//			}
	//		}
	//	}
}

class FCDialog extends RDialog
{
	private static final long serialVersionUID = 180043008923741736L;
	static final int columns = 10;
	CategoryData categoryData;
	ArrayList<FoodData> list;
	ArrayList<FCPanel> fcPanels;
	public FCDialog(Window window, CategoryData categoryData)
	{
		super(window);
		this.categoryData = categoryData;
		list = categoryData.getAllDatas();

		setTransBrightStyle(true);
		updateTitile();
		setTitleFont(FontTools.getCustomFont(18));
		setTitleColor(Color.green);
		setTitleHeight(35);
		setSection(15);
		addMouseListener(new MouseAdapter()
		{
			@Override
			public void mouseClicked(MouseEvent e)
			{
				WindowTools.hideWindowToBorderInThread(FCDialog.this,
						Direction.UP);
			}
		});
		Container contentPane = getContentPane();
		contentPane.setLayout(new GridLayout(0, 10, 5, 5));
		fcPanels = new ArrayList<FCPanel>();
		int size = list.size();
		for (int i = 0; i < size; i++)
		{
			FCPanel panel = new FCPanel();
			contentPane.add(panel);
			fcPanels.add(panel);
		}
		pack();
		categoryData
				.addDataAddedActor((FoodData foodData, Boolean isBreakfast) ->
				{
					new Thread(() ->
					{
						addNew();
					}).start();
				});

		categoryData.addDataDeletedActor((Integer index, Boolean isBreakfast) ->
		{
			new Thread(() ->
			{
				delete();
			}).start();
		});

		categoryData.addNameChangedOperater((String name) ->
		{
			setTitle("〖" + name + "〗喜爱度排名");
		});
	}

	public void updateData()
	{
		int[] datas = categoryData.findFavorite();
		int i = 0;
		for (FCPanel panel : fcPanels)
		{
			panel.setFoodData(datas[i] == -1 ? null : list.get(datas[i]));
			i++;
		}
	}
	public void addNew()
	{
		int l = list.size() - fcPanels.size();
		for (int i = 0; i < l; i++)
		{
			FCPanel panel = new FCPanel();
			add(panel);
			fcPanels.add(panel);
		}
		pack();
	}

	public void updateTitile()
	{
		setTitle("〖" + categoryData.getCategoryName() + "〗喜爱度排名");
	}

	public void delete()
	{
		int size = fcPanels.size();
		if (list.size() < size)
		{
			remove(fcPanels.get(size - 1));
			fcPanels.remove(size - 1);
		}
		pack();
	}
}

class FCPanel extends FoodComp implements Comparable<FCPanel>
{
	private static final long serialVersionUID = 3887748165637865105L;
	private static Dimension size /* = new Dimension(90, 80) */;
	private static PositionConfiger positionConfiger = new PositionConfiger();

	static
	{
		int w = (ScreenTools.getDefaultScreenSize().width - 280)
				/ FCDialog.columns - 5;
		int h = (int) (w * 0.88);
		size = new Dimension(w, h);
		positionConfiger.updatePramater(w, h, 0);
	}

	private final IRichLabel richLable = new IRichLabel();
	private int totalCount;
	private Operator<String> nameChangedOperator;
	public FCPanel()
	{
		super(positionConfiger);
		initGUI();
		nameChangedOperator = (String o) ->
		{
			foodNameLabel.setText(o);
		};
	}

	private void initGUI()
	{
		richLable.setIconTextGap(1);
		richLable.setForeground(Color.red);
		richLable.setFont(FontTools.getSystemFont(15));
		add(richLable);
		setPreferredSize(size);
	}

	public void setFoodData(FoodData newData)
	{
		if (newData != null)
		{
			if (foodData != newData)
			{
				if (foodData != null)
					foodData.removeNameChangedOperater(nameChangedOperator);
				newData.addNameChangedOperater(nameChangedOperator);
				setImage(newData.getThumbnail());
				foodNameLabel.setText(newData.getFoodName());
			}
			int n = newData.getTotalCount();
			if (totalCount != n)
			{
				totalCount = n;
				if (n > 0)
				{
					richLable.setIcon(Resource.heartIcon);
					richLable.setText(String.valueOf(n));
				} else
				{
					richLable.setIcon((Icon) null);
					richLable.setText("");
				}
			}
		} else
		{
			setImage(null);
			foodNameLabel.setText("");
			richLable.setIcon((Icon) null);
			richLable.setText("");
		}
		foodData = newData;
	}

	public void updateImage()
	{
		if (getImage() == null)
			setImage(foodData.getThumbnail());
	}

	@Override
	public void setBounds(int x, int y, int width, int height)
	{
		super.setBounds(x, y, width, height);
		richLable.setBounds(1, 1, 50, 20);
	}

	@Override
	public int compareTo(FCPanel o)
	{
		return foodData.getTotalCount() - o.foodData.getTotalCount();
	}
}
